import random
import time

import pyautogui

# gui暂停时间
pyautogui.PAUSE = 0.5

#  暂停时间
sleepTime = 3

# 上一次鼠标位置
mousePositionX = 0
mousePositionY = 0

def new_func(mousePositionX, mousePositionY, xCurrent, yCurrent):
    return xCurrent != mousePositionX or yCurrent != mousePositionY


while True:
    time.sleep(sleepTime)
    # 获取当前鼠标的位置
    now = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    xCurrent, yCurrent = pyautogui.position()
    positionStr = now + ' 鼠标坐标点（X,Y）为：{},{}'.format(str(xCurrent).rjust(4), str(yCurrent).rjust(4))
    print(positionStr)

    # 判断鼠标是否发生移动，如果发生位移，则取消本次操作
    new_var = new_func(mousePositionX, mousePositionY, xCurrent, yCurrent)
    if new_var:
        print("上次鼠标位置：{}, {}".format(mousePositionX, mousePositionY) )
        mousePositionX = xCurrent
        mousePositionY = yCurrent
        sleepTime = 120
        continue
    
    sleepTime = 30
    check = False
    while check is False:
        x = random.randint(1,10)
        y = random.randint(1,10)
        check = pyautogui.onScreen(x, y)
        print("下一个位置{}，{}，是否在屏幕中：{}".format(x, y, check))

    try:
        pyautogui.moveRel(x,y,duration=0.1)
        pyautogui.click(xCurrent, yCurrent,duration=0.1)
    except KeyboardInterrupt: # 处理 Ctrl-C 按键
        print(r'Done.')
        break
    except:
        print('好家伙！竟然有错，不过没关系')


if __name__ == '__main__':
    new_func()


